package com.shujia.io.eight;

import org.junit.Test;

import java.io.*;

public class ObjetStreamDemo {


    @Test
    public void test2() {
        // 反序列化，从磁盘或网络读取对象到内存
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("file\\obj"))) {
            // 序列化和反序列化的顺序要保持一次
            System.out.println(ois.readByte());
            System.out.println(ois.readShort());
            System.out.println(ois.readInt());
            System.out.println(ois.readBoolean());

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Test
    public void test1() {
        // 序列化，把对象写入到磁盘或网络
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("file\\obj"))) {
            oos.writeByte(1);
            oos.writeShort(2);
            oos.writeInt(3);
            oos.writeBoolean(true);

            // 隐藏的close, 会触发刷新操作

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
